# 用协程实现一个计算平均数的函数


def coro_avg():
    total = 0
    lenght = 0
    while True:
        try:
            value = yield total/lenght
        except ZeroDivisionError:
            value = yield 0
        print(f"got send value {value}")
        total += value
        lenght += 1


my_avg = coro_avg()
print(my_avg)
next(my_avg)
print(my_avg.send(8))
print(my_avg.send(9))
print(my_avg.send(10))

# 编写asyncio异步程序
import asyncio
import time


async def ptime():
    t = time.time()
    await asyncio.sleep(1)
    print("the time:", t)


# 获取名单/消息队列
loop = asyncio.get_event_loop()
# 包装任务
tasks_array = []
for i in range(100):
    tasks_array.append(ptime())
loop.run_until_complete(asyncio.wait(tasks_array))
loop.close()

